const app = getApp();
var funcs = require('../../static/js/funcs.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    topics:[],
    formRes:[],
    totalTopics: 16,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log("onload触发")
    var params = decodeURIComponent(options.params);
    var data = JSON.parse(params);
    console.log("跳转后收到的值：")
    console.log(data)
    let groupid = data.groupid
    let memberid = data.memberid
    let name = data.name
    let groupname = data.groupname
    let inst = data.inst
    let post = data.post
    let that = this
    let openid = app.globalData.openid
    let getdetailflag = data.getdetailflag
    let reassessflag = data.reassessflag
    this.setData({
      getdetailflag:getdetailflag,
      reassessflag:reassessflag
    })
    console.log(reassessflag)
    console.log(getdetailflag)

    funcs.getGroupDetail(openid,groupid).then(res=>{
      console.log(res.data.data)
      that.setData({
        showType: res.data.data.type
      })
      if(getdetailflag==null&&reassessflag==null){
        console.log("普通的评价功能点")
        funcs.getTopiclist(openid,groupid).then(res=>{
          if(res.data.succeed){
            console.log(res.data.data.topics)
            that.setData({
              groupid:groupid,
              topics:res.data.data.topics,
              totalTopics: res.data.data.topics.length,
              name:name,
              groupname:groupname,
              inst:inst,
              post:post,
              memberid:memberid
            })
          }
        })
      }else if(getdetailflag!=null){
        console.log("进入详情功能点")
        funcs.getTopiclistDetail(openid,groupid,memberid).then(res=>{
          if(res.data.succeed){
            console.log(res.data.data.topics)
            that.setData({
              groupid:groupid,
              topics:res.data.data.topics,
              totalTopics: res.data.data.topics.length,
              name:name,
              groupname:groupname,
              inst:inst,
              post:post,
              memberid:memberid
            })
          }
        })
      }else if(reassessflag!=null){
        console.log("进入重新评估功能点")
        funcs.clearAssess(openid,groupid,memberid).then(res=>{
          if(res.data.succeed){
            console.log("清理成功")
            funcs.getTopiclist(openid,groupid).then(res=>{
              if(res.data.succeed){
                console.log(res.data.data.topics)
                that.setData({
                  groupid:groupid,
                  topics:res.data.data.topics,
                  totalTopics: res.data.data.topics.length,
                  name:name,
                  groupname:groupname,
                  inst:inst,
                  post:post,
                  memberid:memberid
                })
              }
            })
          }
        })
      }
    })



  },

  radioChange(e) {
    let openid = app.globalData.openid
    let data = e.currentTarget.dataset;
    let assess = {
      topic_id:Number(data.topicid),
      member_id:Number(this.data.memberid),
      group_id:Number(this.data.groupid),
      staff_id:openid,
      answer_type:e.detail.value
    }
    console.log("加载的assess为",assess)
    let currlist = this.data.formRes
    console.log("移除前为：",currlist)
    const newArray = currlist.filter(item => item.topic_id !== data.topicid); // 移除
    console.log("移除后为：",newArray)
    newArray.push(assess)
    this.setData({
      formRes:newArray
    })
    console.log("当前formRes>",this.data.formRes)
  },

  submitForm(res){
    let openid = app.globalData.openid
    let count = 0
    for(let i=0; i<this.data.formRes.length;i++){
      if(this.data.formRes[i].answer_type=="ok"){
        count +=1;
      }
    }
    if(count<2){
      wx.showToast({
        title: "有差距的评价过少",
        icon: 'none',
        duration: 3000
      })
      return
    }

    funcs.submitForm(this.data.formRes).then(res=>{
      if(res.data.succeed){
        wx.showToast({
          title: "提交成功",
          duration: 2000
        })
        wx.reLaunch({
          url: '/pages/assess/assess',
        })
      }else{
        wx.showToast({
          title: "请重新核对",
          icon: 'error',
          duration: 2000
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})